package com.eat.fish_service.controller.web;

import com.eat.common_utils.R;
import com.eat.fish_service.entity.Dao.Fish;
import com.eat.fish_service.entity.VO.EditChargeVo;
import com.eat.fish_service.entity.VO.IpPortVo;
import com.eat.fish_service.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author CZF
 * @create 2022-05-01 21:34
 */

@RestController("webAquariumController")
@RequestMapping("/web/aquarium")
@Api(tags = "web端鱼缸管理")
@CrossOrigin
public class AquariumController {

    @Autowired
    private FishService fishService;

    @Autowired
    private AquariumService aquariumService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private UserAquariumService userAquariumService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "返回当前用户所有水族箱id")
    @GetMapping("aquariumIdList/{username}")
    public R aquariumIdList(@PathVariable("username") String username) {
        return userAquariumService.aquariumIdList(username);
    }


    @ApiOperation(value = "返回水族箱信息(已连接的鱼缸)， 参数：鱼缸id")
    @GetMapping("aquariumMess/{aquariumId}")
    public R aquariumMess(@PathVariable("aquariumId") Long aquariumId) {
        return aquariumService.aquariumMess(aquariumId);
    }

    @ApiOperation(value = "返回给管理员数据库中所有水族箱信息(已连接+未连接)")
    @GetMapping("allAquariumMess")
    public R allAquarium() {
        return aquariumService.allAquariumMess();
    }

    //管理员添加鱼缸，参数：ip地址、端口号
    @ApiOperation(value = "管理员添加鱼缸，参数1（url参数携带）：负责人的用户名 参数2（请求体中）：ip地址、端口号")
    @PostMapping("addEquipment/{charge}")
    public R addEquipment(@PathVariable("charge") @ApiParam(name = "charge", value = "负责人的用户名", required = true) String charge,
                          @RequestBody @ApiParam(name = "ipPortVo", value = "ip地址、端口号", required = false) IpPortVo ipPortVo) {
        try {
            return aquariumService.addEquipment(charge, ipPortVo);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("事务回滚！添加失败");
        }
    }

    @ApiOperation(value = "更改鱼的图片，根据鱼的id把图片更新到鱼表的对应列，修改鱼的信息时调用")
    @PostMapping("uploadFishPic/{fishId}")
    public R uploadFishPic(MultipartFile pic,
                           @ApiParam(name = "fishId", value = "鱼的id", required = true)
                           @PathVariable("fishId") String fishId) {
        return fishService.uploadFishPic(pic, fishId);
    }

    @ApiOperation(value = "上传鱼的图片，返回图片路径，添加鱼的时候调用")
    @PostMapping("addFishPic")
    public R uploadFishPic(MultipartFile pic) {
        return fishService.addFishPic(pic);
    }

    @ApiOperation(value = "添加鱼类 参数1：鱼缸id  参数2：鱼的信息 返回鱼的id")
    @PostMapping("addFish/{aquariumId}")
    public R addFish(@PathVariable("aquariumId")
                     @ApiParam(name = "aquariumId", value = "鱼缸的id", required = true) Long aquariumId,
                     @RequestBody @ApiParam(value = "鱼的信息 返回鱼的id", required = false) Fish fish) {
        return fishService.addFish(aquariumId, fish);
    }

    @ApiOperation(value = "修改某条鱼的信息")
    @PostMapping("editFish")
    public R editFish(@RequestBody Fish fish) {
        return fishService.editFish(fish);
    }

    @ApiOperation(value = "删除某条鱼的记录，同时鱼缸鱼的数目减1")
    @GetMapping("deleteFish/{aquariumId}/{fishId}")
    public R deleteFish(@PathVariable("aquariumId")
                        @ApiParam(name = "aquariumId", value = "鱼缸的id", required = true) Long aquariumId,
                        @PathVariable("fishId")
                        @ApiParam(name = "fishId", value = "鱼的id", required = true) String fishId) {
        return fishService.deleteFish(aquariumId, fishId);

    }

    @ApiOperation(value = "开关设备,参数1：鱼缸id 参数2:设备名称 参数3：0：关闭  1：打开")
    @GetMapping("operateEquipment/{aquariumId}/{type}/{behavior}")
    public R operateEquipment(@PathVariable("aquariumId")
                              @ApiParam(name = "aquariumId", value = "鱼缸的id", required = true) Long aquariumId,
                              @PathVariable("type") @ApiParam(name = "type", value = "设备名称", required = true) String type,
                              @PathVariable("behavior") @ApiParam(name = "behavior", value = "开关，0：关闭  1：打开", required = true) byte behavior) {
        return equipmentService.operateEquipment(aquariumId, type, behavior);
    }

    //根据鱼缸id获取鱼缸所有鱼的信息
    @ApiOperation(value = "根据鱼缸id获取鱼缸所有鱼的信息")
    @GetMapping("getAllFish/{aquariumId}")
    public R getAllFish(@PathVariable("aquariumId") Long aquariumId) {
        return fishService.getAllFish(aquariumId);
    }

    //修改鱼缸最大数量
    @ApiOperation(value = "修改鱼缸的最大数量")
    @GetMapping("editMaxFishNum/{aquariumId}/{maxNum}")
    public R editMaxFishNum(@PathVariable("aquariumId") Long aquariumId, @PathVariable("maxNum") Integer maxNum) {
        return aquariumService.editMaxFishNum(aquariumId, maxNum);
    }

    //修改负责人信息
    @ApiOperation(value = "修改负责人信息,负责人是负责鱼缸的管理员，鱼缸出任何问题，都可以去找负责人，比如设备坏了、")
    @PostMapping("editCharge")
    public R editCharge(@RequestBody EditChargeVo editChargeVo) {
        return userService.editCharge(editChargeVo);
    }

    @ApiOperation(value = "查询设备状态")
    @GetMapping("queryEquipmentStatus/{aquariumId}")
    public R queryEquipmentStatus(@PathVariable Long aquariumId) {
        return equipmentService.queryEquipmentStatus(aquariumId);
    }
}
